home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4810 / 4810.xpi / components / speeddialcache.js
Text File  |  2009-03-26  |  5KB  |  161 lines

  1. const nsISupports = Components.interfaces.nsISupports;
  2. const nsIDOMEventTarget = Components.interfaces.nsIDOMEventTarget;
  3.  
  4. // You can change these if you like
  5. const CLASS_ID = Components.ID("7a44e0ce-757a-4f65-93aa-b05a377558a7");
  6. const CLASS_NAME = "c-speeddial";
  7. const CONTRACT_ID = "@uworks.net/speeddialcache;1";
  8. const INTERFACE = Components.interfaces.nsISupports;
  9.  
  10. // This is your constructor.
  11. // You can do stuff here.
  12. function SpeedDialCache() {
  13.   // you can cheat and use this
  14.   // while testing without
  15.   // writing your own interface
  16.   this.wrappedJSObject = this;
  17. }
  18.  
  19. // This is the implementation of your component.
  20. SpeedDialCache.prototype = {
  21.   imageArray: new Array(),
  22.   hiddenWindow: null,
  23.   currentInstances: 0,
  24.  
  25.   // for nsISupports
  26.   QueryInterface: function(aIID)
  27.   {
  28.     // add any other interfaces you support here
  29.     if (!aIID.equals(INTERFACE) && !aIID.equals(nsISupports))
  30.         throw Components.results.NS_ERROR_NO_INTERFACE;
  31.     return this;
  32.   },
  33.  
  34.   getImage: function(targetDial)
  35.   {
  36.     if ((targetDial >= this.imageArray.length) || (!this.imageArray[targetDial])) {
  37.       return null;
  38.     } else {
  39.       return this.imageArray[targetDial];
  40.     }
  41.   },
  42.   
  43.   hasImage: function(targetDial)
  44.   {
  45.     return ((targetDial < this.imageArray.length) && (this.imageArray[targetDial]));
  46.   },
  47.  
  48.   setImage: function(targetImageURL, targetDial) {
  49.     if (targetImageURL == null) {
  50.       this.imageArray[targetDial] = null;
  51.     } else {
  52.       if (this.hiddenWindow == null) {
  53.         this.hiddenWindow = Components.classes["@mozilla.org/appshell/appShellService;1"]
  54.            .getService(Components.interfaces.nsIAppShellService)
  55.            .hiddenDOMWindow;
  56.          this.hiddenWindow.Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  57.            .getService(Components.interfaces.mozIJSSubScriptLoader)
  58.            .loadSubScript("chrome://speeddial/content/hiddenWindow.js");
  59.       }
  60.       var newImage = this.hiddenWindow.speedDialCreateImage();
  61.       newImage.imageLoaded = false;
  62.       newImage.addEventListener("load", function(event) {
  63.         if (event.currentTarget.notifyDials != null) {
  64.           for (var c=0; c<event.currentTarget.notifyDials.length; c++) {
  65.             try {
  66.               event.currentTarget.notifyDials[c].imageLoadComplete(event);
  67.             } catch (e) {
  68.               Components.utils.reportError("Error calling imageLoadComplete: " + e);
  69.             }
  70.           }
  71.           event.currentTarget.notifyDials.length = 0;
  72.           event.currentTarget.notifyDials = null;
  73.         }
  74.         event.currentTarget.imageLoaded = true;
  75.       }, true);
  76.       
  77.       if (this.imageArray[targetDial]) {
  78.         if (this.imageArray[targetDial].notifyDials) {
  79.           newImage.notifyDials = this.imageArray[targetDial].notifyDials;
  80.           this.imageArray[targetDial].notifyDials = null;
  81.         }
  82.         if ((this.imageArray[targetDial].imageLoaded) ||
  83.             (this.imageArray[targetDial].width != 0)) {
  84.           newImage.prevWidth = this.imageArray[targetDial].width;
  85.           newImage.prevHeight = this.imageArray[targetDial].height;
  86.         } else {
  87.           newImage.prevWidth = this.imageArray[targetDial].prevWidth;
  88.           newImage.prevHeight = this.imageArray[targetDial].prevHeight;
  89.         }
  90.       }
  91.       
  92.       newImage.src = targetImageURL;
  93.       
  94.       this.imageArray[targetDial] = newImage;
  95.     }
  96.   },
  97.  
  98.   setImageObject: function(targetImage, targetDial) {
  99.     this.imageArray[targetDial] = targetImage;
  100.   },
  101.  
  102.   getNumberInstances: function() {
  103.     return this.currentInstances;
  104.   },
  105.  
  106.   addInstance: function() {
  107.     this.currentInstances++;
  108.   },
  109.  
  110.   removeInstance: function() {
  111.     this.currentInstances--;
  112.   }
  113. }
  114.  
  115. //=================================================
  116. // Note: You probably don't want to edit anything
  117. // below this unless you know what you're doing.
  118. //
  119. // Factory
  120. var SpeedDialCacheFactory = {
  121.   singleton: null,
  122.   createInstance: function (aOuter, aIID)
  123.   {
  124.     if (aOuter != null)
  125.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  126.     if (this.singleton == null)
  127.       this.singleton = new SpeedDialCache();
  128.     return this.singleton.QueryInterface(aIID);
  129.   }
  130. };
  131.  
  132. // Module
  133. var SpeedDialCacheModule = {
  134.   registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  135.   {
  136.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  137.     aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
  138.   },
  139.  
  140.   unregisterSelf: function(aCompMgr, aLocation, aType)
  141.   {
  142.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  143.     aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);        
  144.   },
  145.   
  146.   getClassObject: function(aCompMgr, aCID, aIID)
  147.   {
  148.     if (!aIID.equals(Components.interfaces.nsIFactory))
  149.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  150.  
  151.     if (aCID.equals(CLASS_ID))
  152.       return SpeedDialCacheFactory;
  153.  
  154.     throw Components.results.NS_ERROR_NO_INTERFACE;
  155.   },
  156.  
  157.   canUnload: function(aCompMgr) { return true; }
  158. };
  159.  
  160. //module initialization
  161. function NSGetModule(aCompMgr, aFileSpec) { return SpeedDialCacheModule; }